package vn.personal.anhht.bloback12.activity;

import java.util.ArrayList;

import vn.personal.anhht.bloback12.adapter.LogItemAdapter;
import vn.personal.anhht.bloback12.bean.LogObject;
import vn.personal.anhht.bloback12.util.UtilMethods;
import vn.personal.anhht.bloback12.widget.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.CallLog.Calls;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class TabLogCallActivity extends Activity{

	//CallLogAdapter adapter = null;
	private LogItemAdapter adapterLog;
	private ListView lvLogs;
	private ArrayList<LogObject> listCallLog = new ArrayList<LogObject>();
	ProgressDialog dialog;
	private Resources rs;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		//LocalizationUtil.setupLanguage(getApplicationContext());
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_log_call);
		rs = getResources();
		ActionBar actionBar = (ActionBar)findViewById(R.id.actionbar);
		actionBar.setVisibility(View.GONE);
		Log.e("LOGCALL TAB", "LOGCALL TAB");
		initLogCall();
	}
	
	public void initLogCall(){
		dialog = new ProgressDialog(this);
		lvLogs = (ListView) findViewById(R.id.lvcalllog);
		adapterLog = new LogItemAdapter(getApplicationContext(),
				android.R.layout.simple_list_item_1, listCallLog);
		new GetLogThread().execute();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mn_filter, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.item_filter_clear: {
			displayDialog();
			break;
		}
		default:
			super.onOptionsItemSelected(item);
		}
		return true;
	}

	public void refresh() {
		listCallLog.clear();
		new GetLogThread().execute();
	}
	
	public void displayDialog(){
		//final String word = listWords.get(position);
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle(getResources().getString(R.string.history_alert_title));
		alert.setMessage(getResources().getString(R.string.history_alert_msg));
		alert.setPositiveButton(getResources().getString(R.string.ok) , new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				deleteLogCall();
				Toast.makeText(getApplicationContext(), getResources().getString(R.string.history_toast_delok),
						Toast.LENGTH_LONG).show();
				refresh();
			}
		});

		alert.setNegativeButton(getResources().getString(R.string.cancel),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.cancel();
					}
				});
		alert.show();
	}
	
	/*
	 * AnhHT:
	 */
	public void deleteLogCall() {
		Cursor c = getApplicationContext().getContentResolver().query(
				android.provider.CallLog.Calls.CONTENT_URI, null, null,
				null, null);
		while (c.moveToNext()) {
			try {
				getApplicationContext().getContentResolver().delete(
						Calls.CONTENT_URI, null, null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		c.close();
	}
	
	/* ========================Get List Log============================= */
	public class GetLogThread extends
			AsyncTask<Void, Void, Void> {
		@Override
		protected Void doInBackground(Void... params) {
			queryCallIncomed();
			return null;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog.setMessage(rs.getString(R.string.bl_dialog_load));
			dialog.show();
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			if (dialog.isShowing()) {
				dialog.dismiss();
			}
			lvLogs.setAdapter(adapterLog);
		}

		/*
		 * AnhHT:
		 */
		private void queryCallIncomed() {
			
			String[] strFields = { android.provider.CallLog.Calls.CACHED_NAME,
					android.provider.CallLog.Calls.DATE,
					android.provider.CallLog.Calls.NUMBER,
					android.provider.CallLog.Calls.TYPE };
			String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
			Cursor callCursor = getContentResolver().query(
					android.provider.CallLog.Calls.CONTENT_URI, strFields,
					null, null, strOrder);
			if (callCursor.moveToFirst()) {
				String mynumber = null;
				String myname = null;
				String mytype = null;
				String mydate = null;
				int indexOfNumber = callCursor.getColumnIndex(Calls.NUMBER);
				int indexOfCachedName = callCursor.getColumnIndex(Calls.CACHED_NAME);
				int indexOfDateCall = callCursor.getColumnIndex(Calls.DATE);
				int indexOfTypeCall = callCursor.getColumnIndex(Calls.TYPE);
				do {
					LogObject lo = new LogObject();
					mynumber = callCursor.getString(indexOfNumber);
					if (mynumber == null) {
						lo.setPhone(rs.getString(R.string.pick_string_nophone));
					} else {
						lo.setPhone(mynumber);
					}
					myname = callCursor.getString(indexOfCachedName);
					if (myname == null) {
						lo.setName(rs.getString(R.string.pick_string_noname));
					} else {
						lo.setName(myname);
					}
					mydate = callCursor.getString(indexOfDateCall);
					lo.setTime(UtilMethods
							.changeMilisecondToDate(Long.parseLong(mydate)));
					mytype = callCursor.getString(indexOfTypeCall);
					lo.setType(Integer.parseInt(mytype.trim()));
					listCallLog.add(lo);
				} while (callCursor.moveToNext());
			}
			callCursor.close();
		}
	}
}
